﻿using Microsoft.TeamFoundation.Build.Workflow.Tracking;

namespace TfsCloudDeploy.Workflow.Core
{
    using System.Activities;
    using System.Activities.Expressions;
    using System.Activities.XamlIntegration;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using Microsoft.TeamFoundation.Build.Client;
    using Microsoft.TeamFoundation.Build.Workflow.Activities;

    public static class WorkflowUtility
    {
        /// <summary>
        /// Returns the list of process parameters from the template. Values for those parameters are only
        /// the default values. Actual overrides are in the build definition's ProcessParameters serialized
        /// XML collection.
        /// </summary>
        /// <param name="buildDefinition"></param>
        /// <returns></returns>
        public static List<DynamicActivityProperty> GetPropertyMetadata(IBuildDefinition buildDefinition)
        {
            StringReader sr = new StringReader(buildDefinition.Process.Parameters);
            DynamicActivity activity = (DynamicActivity)ActivityXamlServices.Load(sr);
            IEnumerable<DynamicActivityProperty> propertyQuery = from dap in activity.Properties
                                                                 select dap;
            return propertyQuery.ToList<DynamicActivityProperty>();
        }

        public static bool GetBoolValue(object value)
        {
            InArgument<bool> inArgument = value as InArgument<bool>;
            if (inArgument != null)
            {
                if (inArgument.Expression is Literal<bool>)
                {
                    Literal<bool> boolExpression = inArgument.Expression as Literal<bool>;
                    return boolExpression.Value;
                }
                //add handling for more expression subclasses here...
            }
            return false;
        }

        public static int GetInt32Value(object value)
        {
            InArgument<int> inArgument = value as InArgument<int>;
            if (inArgument != null)
            {
                if (inArgument.Expression is Literal<int>)
                {
                    Literal<int> intExpression = inArgument.Expression as Literal<int>;
                    return intExpression.Value;
                }
                //add handling for more expression subclasses here...
            }
            return 0;
        }

        public static string GetStringValue(object value)
        {
            InArgument<string> inArgument = value as InArgument<string>;
            if (inArgument != null)
            {
                if (inArgument.Expression is Literal<string>)
                {
                    Literal<string> intExpression = inArgument.Expression as Literal<string>;
                    return intExpression.Value;
                }
                //add handling for more expression subclasses here...
            }
            return string.Empty;
        }

        public static string[] GetStringArrayValue(object value)
        {
            InArgument<string[]> inArgument = value as InArgument<string[]>;
            if (inArgument != null)
            {
                if (inArgument.Expression is Literal<string[]>)
                {
                    Literal<string[]> intExpression = inArgument.Expression as Literal<string[]>;
                    return intExpression.Value;
                }
                //add handling for more expression subclasses here...
            }
            return new string[0];
        }

        public static void CreateBuildMessage(string messageText, CodeActivityContext context)
        {
            CreateBuildMessage(messageText, context, BuildMessageImportance.Low);
        }

        public static void CreateBuildMessage(string messageText, CodeActivityContext context, BuildMessageImportance buildMessageImportance)
        {
            context.Track(new BuildInformationRecord<BuildMessage>()
            {
                Value = new BuildMessage()
                {
                    Importance = buildMessageImportance,
                    Message = messageText,
                },
            });
        }

    }
}
